package com.itheima.controller.impl;

import com.itheima.controller.StudentController;
import com.itheima.domain.Student;
import com.itheima.uitls.AgeUtils;

import java.util.ArrayList;

public class StudentControllerImpl implements StudentController {
    @Override
    public void start() {
        while (true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choose = sc.nextLine();
            switch (choose){
                case "1":
                    addStudent();
                    break;
                case "2":
                    delectStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudnet();
                    break;
            }

        }
    }

    @Override
    public void addStudent() {
        while (true){
            System.out.println("请输入要添加的学生id:");
            String id = sc.nextLine();
            int index = studentService.findStudentById(id);
            if (index == -1){
                Student student = inputStudent("添加",id);
                studentService.addStudent(student);
                System.out.println("添加成功");
                break;
            }else{
                System.out.println("该id不存在,请重新输入");
            }
        }

    }

    public Student inputStudent(String msg,String id) {
        System.out.println("请输入要"+ msg +"的学生姓名:");
        String name = sc.nextLine();
//                System.out.println("请输入要添加的学生年龄:");
//                String age = sc.nextLine();
        System.out.println("请输入要"+ msg +"的学生生日:");
        String birthday = sc.nextLine();
        String age = AgeUtils.getAge(birthday);
        return new Student(id,name,age,birthday);
    }

    @Override
    public void delectStudent() {
        //while (true){
            System.out.println("请输入要删除的学生id:");
            String id = sc.nextLine();
            int index = studentService.findStudentById(id);
            if (index != -1){
               studentService.delectStudent(index);
                System.out.println("删除成功");
            }else{
                System.out.println("该id不存在");
            }
       // }
    }

    @Override
    public void findAllStudnet() {
        ArrayList<Student> allStudent = studentService.findAllStudent();
        if (allStudent.size() == 0){
            System.out.println("当前系统无数据,请先添加");
        }else {
            System.out.println("学号\t\t姓名\t年龄\t\t生日");
            for (Student stu : allStudent) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    @Override
    public void updateStudent() {
        while (true){
            System.out.println("请输入要修改的学生id:");
            String id = sc.nextLine();
            int index = studentService.findStudentById(id);
            if (index != -1){
                Student student = inputStudent("修改",id);
                studentService.updateStudent(index,student);
                break;
            }else{
                System.out.println("该id已存在,请重新输入");
            }
        }
    }
}
